﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PlanningGIS.ArcGIS;
using System.IO;
using PlanningGIS.Util;
using DevComponents.DotNetBar;
using ESRI.ArcGIS.Display;
using PlanningGIS.Util.Drawing;
using PlanningGIS.Engine.Controls;
using System.Drawing.Text;

namespace PlanningGIS.Symbol
{
    /// <summary>
    /// 面符号控件
    /// </summary>
    public partial class PolyStyle : UserControl
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public PolyStyle()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 加载窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StyleCreator_Load(object sender, EventArgs e)
        {
            
        }

        /// <summary>
        /// 符号构建处理类
        /// </summary>
        private StyleCreator _Creator = null;

        /// <summary>
        /// 已选择的符号对象
        /// </summary>
        public ISymbol SelectedSymbol { get; set; }

        /// <summary>
        /// 初始化控件
        /// </summary>
        public void InitControl()
        {
            if (_Creator == null)
            {
                _Creator = new StyleCreator();
                _Creator.StylePanel = itmpnlStyles;
                _Creator.StyleCommand = cmdStyle;
                _Creator.AddFiles(cmbStyles);
            }
            try
            {
                UpdateSymbol();
                UpdateImage(); 
                _Creator?.SetSelected(SelectedSymbol);
            }
            catch
            {

            }
        }

        /// <summary>
        /// 
        /// </summary>
        private void UpdateSymbol()
        {
            IFillSymbol sym = (IFillSymbol)SelectedSymbol;
            clrFill.SelectedColor = Convertor.IColor2Color(sym.Color);
            ILineSymbol l = sym.Outline;
            iptFWidth.Value = l.Width;
            clrBorder.SelectedColor = Convertor.IColor2Color(l.Color);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddStyle_Click(object sender, EventArgs e)
        {
            if (opdlgStyle.ShowDialog(this.ParentForm) != DialogResult.OK) return;
            _Creator?.CreateStyles(zjgisStyleGalleryType.FillSymbols, opdlgStyle.FileName);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbStyles_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                _Creator?.CreateStyles(zjgisStyleGalleryType.FillSymbols, cmbStyles.SelectedValue.ToString());
            }
            catch
            {

            }
           
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmdStyle_Executed(object sender, EventArgs e)
        {
            ButtonItem btn = (ButtonItem)sender;
            picPreview.Image = btn.Image;
            SelectedSymbol = (ISymbol)btn.Tag;
            UpdateSymbol();
        }

        /// <summary>
        /// 
        /// </summary>
        private void UpdateImage()
        {
            picPreview.Image = SymbolHelper.DrawSymbol(SelectedSymbol, picPreview.Width, picPreview.Height);
        }
        
        #region 面
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clrFill_SelectChanged(object sender, ColorEventArgs e)
        {
            if (!(SelectedSymbol is IFillSymbol)) return;
            (SelectedSymbol as IFillSymbol).Color = Convertor.Color2IColor(clrFill.SelectedColor);
            UpdateImage();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void iptFWidth_ValueChanged(object sender, EventArgs e)
        {
            if (!(SelectedSymbol is IFillSymbol)) return;
            ILineSymbol lsym = (SelectedSymbol as IFillSymbol).Outline;
            lsym.Width = iptFWidth.Value;
            (SelectedSymbol as IFillSymbol).Outline = lsym;
            UpdateImage();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clrBorder_SelectChanged(object sender, ColorEventArgs e)
        {
            if (!(SelectedSymbol is IFillSymbol)) return;
            ILineSymbol lsym = (SelectedSymbol as IFillSymbol).Outline;
            lsym.Color = Convertor.Color2IColor(clrBorder.SelectedColor);
            (SelectedSymbol as IFillSymbol).Outline = lsym;
            UpdateImage();
        }

        #endregion
        
    }
}
